package com.fizzkai.p2p.bussness.service.impl;

import com.fizzkai.p2p.bussness.domain.ExpAccount;
import com.fizzkai.p2p.bussness.domain.ExpAccountFlow;
import com.fizzkai.p2p.bussness.mapper.ExpAccountFlowMapper;
import com.fizzkai.p2p.bussness.service.IExpAccountFlowService;
import com.fizzkai.p2p.core.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service@Transactional
public class ExpAccountFlowServiceImpl implements IExpAccountFlowService {

    @Autowired
    private ExpAccountFlowMapper expAccountFlowMapper;


    @Override
    public int save(ExpAccountFlow entity) {
        return expAccountFlowMapper.insert(entity);
    }

    @Override
    public int update(ExpAccountFlow entity) {
        return expAccountFlowMapper.updateByPrimaryKey(entity);
    }

    @Override
    public ExpAccountFlow get(Long id) {
        return expAccountFlowMapper.selectByPrimaryKey(id);
    }

    @Override
    public void createRigisterFlow(ExpAccount expAccount, BigDecimal money) {
        createFlow(expAccount, money,  Constants.EXPMONEY_TYPE_REGISTER, "注册发放体验金");
    }

    @Override
    public void createAuditFailFlow(ExpAccount account, BigDecimal money, String remark) {
        createFlow(account, money, Constants.EXPMONEY_FLOW_BID_FAILED, "满审失败，退回体验金");
    }

    @Override
    public void createBidSuccessFlow(ExpAccount account, BigDecimal money, String remark) {
        createFlow(account, money, Constants.EXPMONEY_FLOW_BID_SUCCESS, "投标成功，回收体验金");
    }


    private void createFlow(ExpAccount account, BigDecimal money, int actionType, String note){
        ExpAccountFlow flow = new ExpAccountFlow();
        flow.setExpAccountId(account.getId());
        flow.setUsableAmount(account.getUsableAmount());
        flow.setFreezedAmount(account.getFreezedAmount());
        flow.setActionType(actionType);
        flow.setAmount(money);
        flow.setActionTime(new Date());
        flow.setNote(note);
        this.save(flow);
    }
}
